/*
 * Filename:    WarnInfoServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年11月3日
 */
package com.paic.mhis.hcpms.warning.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.hcpms.warning.biz.service.HcpmsWarnService;
import com.paic.mhis.hcpms.warning.biz.service.WarningInfoJobService;
import com.paic.mhis.hcpms.warning.biz.service.WarningWorkFlowService;
import com.paic.mhis.hcpms.warning.dao.WarningInfoJobDAO;
import com.paic.mhis.hcpms.warning.dao.WarningPoolDAO;
import com.paic.mhis.hcpms.warning.dto.HcpmsWarningDTO;
import com.paic.mhis.hcpms.warning.dto.WarningInfoJobDTO;
import com.paic.mhis.hcpms.warning.dto.WarningParameterJobDTO;
import com.paic.mhis.hcpms.warning.util.WarnConstants;

@Service("warnInfoJobService")
public class WarningInfoJobServiceImpl implements WarningInfoJobService {
	@Autowired
	private WarningInfoJobDAO warnInfoDao;
	@Autowired
	private WarningPoolDAO warnPoolDao;
	@Autowired
	private WarningWorkFlowService warningWorkFlowService;
	@Autowired
	private HcpmsWarnService hcpmsWarnService;

	@Override
	public String getWarnNo() {
		// TODO Auto-generated method stub
		return warnInfoDao.getWarnNo();
	}

	@Transactional
	@Override
	public void insertWarnInfo(WarningParameterJobDTO warningParameter) {
		// TODO Auto-generated method stub
		List<WarningInfoJobDTO> warnPoolData = warnPoolDao.getWarnPoolData(warningParameter);
		for (WarningInfoJobDTO warnInfo : warnPoolData) {
			warnInfo.setWarnNo(warnInfoDao.getWarnNo());
			warnInfo.setSource(WarnConstants.WARN_SOURCE_07);
			warnInfo.setKpiTime(warningParameter.getKpiTime());
		}
		if (warnPoolData.size() > 0) {
			warnInfoDao.insertWarnInfo(warnPoolData);
			warnInfoDao.insertWarnInfoTarget(warnPoolData);
			warnPoolDao.updateWarnPoolTaskNo(warnPoolData);
		}
		for (WarningInfoJobDTO warnInfo : warnPoolData) {
			try {
				// 启动
				String warnNo = warnInfo.getWarnNo();
				Map<String, String> condition = new HashMap<String, String>();
				condition.put("SOURCE", warnInfo.getSource());
				String actCurrent = warningWorkFlowService.startWorkFlow(warnNo, null, condition);
				// 更新状态
				HcpmsWarningDTO warnDto = new HcpmsWarningDTO();
				warnDto.setWarnNo(warnNo);
				warnDto.setStatus(actCurrent);
				hcpmsWarnService.updateHcpmsWarn(warnDto);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
